//
//  AdscrollView.m
//  乐账
//
//  Created by 谢毅 on 17/1/15.
//  Copyright © 2017年 xieyi. All rights reserved.
//

#import "AdscrollView.h"

@interface AdscrollView()
@property(nonatomic,strong)UIImageView *preView;
@property(nonatomic,strong)UIImageView *currentView;
@property(nonatomic,strong)UIImageView *nextView;

@end

@implementation AdscrollView
{
   
    CGFloat viewWidth;//视图宽高
    CGFloat viewHeight;
    NSTimer *timer;   //定时器
    UITapGestureRecognizer *tap;//手势
}

-(UIImageView *)preView{
    if (_preView == nil) {
        _preView = [[UIImageView alloc] init];
    }
    return _preView;
}

-(UIImageView *)currentView{
    if (_currentView == nil) {
        _currentView = [[UIImageView alloc] init];
    }
    return _currentView;
}
-(UIImageView *)nextView{
    if (_nextView == nil) {
        _nextView = [[UIImageView alloc] init];
    }
    return _nextView;
}



- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        viewWidth = self.bounds.size.width;
        viewHeight = self.bounds.size.height;
        //添加滚动视图
        _ad_scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, viewWidth, viewHeight)];
        _ad_scrollview.delegate = self;
        _ad_scrollview.contentSize =CGSizeMake(viewWidth*3, viewHeight);
        _ad_scrollview.showsHorizontalScrollIndicator = NO;
        _ad_scrollview.pagingEnabled = YES;
        _ad_scrollview.backgroundColor = [UIColor darkGrayColor];
        [self addSubview:_ad_scrollview];

        //设置分页
        _pageControl = [[UIPageControl alloc] init];
        _pageControl.userInteractionEnabled = NO;
        _pageControl.currentPageIndicatorTintColor = [UIColor redColor];
        _pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
        [self addSubview:_pageControl];
        //自动布局
        [_pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.mas_centerX);
            make.bottom.equalTo(self.mas_bottom).offset(10);
            make.width.equalTo(self.mas_width);
            make.height.equalTo(@30);
        }];
        //设置单击手势,添加
        tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick)];
        [_ad_scrollview addGestureRecognizer:tap];
        
    }
    [self AutoShow:YES];
    return self;
}
#pragma mark - 单击图片

-(void)tapClick{

   

}
-(void)setImagArray:(NSArray *)imagArray{

    if (imagArray !=nil) {
        _imagArray = imagArray;
        _currentPage = 0;
        _pageControl.numberOfPages = imagArray.count;
    }
//
    NSLog(@"%@",imagArray);
   [self reFlashData];
}
/// 刷新数据view
-(void)reFlashData{

    if ((self.preView && self.currentView && self.
         nextView)) {
//        [_preView removeFromSuperview];
//        [_currentView removeFromSuperview];
//        [_nextView removeFromSuperview];
    }
    /// 从数组取3张图片用于显示
    if (_currentPage == 0) {//第一张的情况
        _preView = [[UIImageView alloc] initWithImage:[_imagArray lastObject]];
        _currentView= [[UIImageView alloc] initWithImage:[_imagArray objectAtIndex:_currentPage]];
        _nextView = [[UIImageView alloc] initWithImage:[_imagArray objectAtIndex:_currentPage + 1]];
    }else if(_currentPage == _imagArray.count - 1){//最后一张的情况
        _preView = [[UIImageView alloc] initWithImage:[_imagArray objectAtIndex:_currentPage -1]];
        _currentView = [[UIImageView alloc] initWithImage:[_imagArray objectAtIndex:_currentPage]];
        _nextView = [[UIImageView alloc] initWithImage:[_imagArray firstObject]];
    }else{
        _preView = [[UIImageView alloc] initWithImage:[_imagArray objectAtIndex:_currentPage -1]];
        _currentView = [[UIImageView alloc] initWithImage:[_imagArray objectAtIndex:_currentPage]];
        _nextView = [[UIImageView alloc] initWithImage:[_imagArray objectAtIndex:_currentPage + 1]];
    }
    
    //设置3个视图的尺寸
    _preView.frame = CGRectMake(0, 0, viewWidth, viewHeight);
    _currentView.frame = CGRectMake(viewWidth, 0, viewWidth, viewHeight);
    _nextView.frame =CGRectMake(viewWidth*2, 0, viewWidth, viewHeight);
    //设置当前分页
    _pageControl.currentPage = _currentPage;
    //显示中间页
    _ad_scrollview.contentOffset = CGPointMake(viewWidth, 0);

}

#pragma mark - 实现scrollview的代理方法
//当滚动视图停顿的时候调用
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    //手动滑动时停止定时器
    [timer invalidate];
    timer = nil;
    timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(autoShowNextImage) userInfo:nil repeats:YES];
    //得到当前偏移
    CGFloat offset = _ad_scrollview.contentOffset.x;
    //向前翻页
    if (offset <= 0) {
        if (_currentPage-1<0) {
            _currentPage = _imagArray.count - 1;
        }else{
        
            _currentPage--;
        }
    }
    //向后翻页
    if(offset >= viewWidth *2){
        if (_currentPage ==_imagArray.count-1) {
            _currentPage = 0;
        }else{
            _currentPage++;
        }
    }

    [self reFlashData];

}
/// 定时滚动
-(void)AutoShow:(BOOL)start{
    if (start) {
        if (!timer) {
            timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(autoShowNextImage) userInfo:nil repeats:YES];
        }
    }else{//关闭自动翻页
        if (timer.isValid) {
            [timer invalidate];
            timer = nil;
        }
    }
}
-(void)autoShowNextImage{

    if (_currentPage == _imagArray.count - 1) {
        _currentPage = 0;
    }else{
        _currentPage ++;
    
    }
    [self reFlashData];
}


@end
